package cn.iocoder.yudao.module.crm.service.task;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.crm.controller.admin.task.vo.TaskDetailReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.task.TaskApproverDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.task.TaskContentDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.tasktype.TaskTypeApproverDO;
import cn.iocoder.yudao.module.crm.dal.dataobject.tasktype.TaskTypeContentDO;
import cn.iocoder.yudao.module.crm.dal.mysql.task.TaskContentMapper;
import cn.iocoder.yudao.module.crm.dal.mysql.tasktype.TaskTypeContentMapper;
import cn.iocoder.yudao.module.crm.enums.task.CrmTaskReviewStatus;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * CRM 任务单内容关联 Service 实现类
 *
 * @author 广州氪量
 */
@Service
@Validated
public class TaskContentServiceImpl implements TaskContentService {
    @Resource
    private TaskContentMapper taskContentMapper;
    @Resource
    private TaskTypeContentMapper taskTypeContentMapper;
    @Override
    public List<TaskContentDO> getTaskContentListByTaskId(Long id) {
        return taskContentMapper.getTaskContentListByTaskId(id);
    }

    @Override
    public void createTaskContent(Long taskTypeId,Long taskId) {
        // 根据任务单类型ID，查询定义的审核流程
        List<TaskTypeContentDO> taskTypeContentDOS = taskTypeContentMapper.selectListByTaskTypeId(taskTypeId);
        // 添加任务单审批人信息
        List<TaskContentDO> taskContentList = new ArrayList<>();
        taskTypeContentDOS.forEach(taskTypeContentDO -> {
            // 根据输入内容方式，确认添加内容
            if(taskTypeContentDO.getInputMethod().contains("1")){
                TaskContentDO taskContentDO = createTaskContentDO(taskId, taskTypeContentDO, "1");
                taskContentList.add(taskContentDO);
            }
            if(taskTypeContentDO.getInputMethod().contains("2")){
                TaskContentDO taskContentDO = createTaskContentDO(taskId, taskTypeContentDO, "2");
                taskContentList.add(taskContentDO);
            }
        });
        // 批量插入
        if (!taskContentList.isEmpty()) {
            taskContentMapper.batchInsert(taskContentList);
        }
    }

    @Override
    public void deleteTaskContent(Long taskId) {
        taskContentMapper.delete(new LambdaQueryWrapperX<TaskContentDO>().eq(TaskContentDO::getTaskId, taskId));
    }

    @Override
    public Long updateTaskContent(TaskDetailReqVO taskDetailReqVO) {
        taskContentMapper.updateBatchByTaskId(taskDetailReqVO);
        return taskDetailReqVO.getTaskId();
    }

    // 设置任务单内容
    private TaskContentDO createTaskContentDO(Long taskId, TaskTypeContentDO taskTypeContentDO, String inputMethod) {
        TaskContentDO taskContentDO = new TaskContentDO();
        taskContentDO.setTaskId(taskId);
        taskContentDO.setName(taskTypeContentDO.getName());
        taskContentDO.setInputMethod(inputMethod);
        taskContentDO.setDetails("");
        return taskContentDO;
    }
}